package com.jml.config;

import com.jml.constant.Constant;
import com.jml.scope.RefreshScope;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;

public class JmlBeanDefinitionRegistry implements BeanDefinitionRegistryPostProcessor {

    /**
     * 目的是为了获取BD
     */
    private BeanDefinitionRegistry beanDefinitionRegistry;

    /**
     * @param beanDefinitionRegistry 就是容器
     */
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
        this.beanDefinitionRegistry = beanDefinitionRegistry;
    }

    /**
     * 这个方法就是往容器中放入东西，是BeanFactoryPostProcessor接口的方法
     */
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
        //设置自定义的scope,放入容器中
        configurableListableBeanFactory.registerScope(Constant.SCOPE_NAME, new RefreshScope());
        configurableListableBeanFactory.registerScope("jmlRefreshzz", new RefreshScope());
    }

    public BeanDefinitionRegistry getBeanDefinitionRegistry() {
        return beanDefinitionRegistry;
    }
}
